package com.stylefeng.guns.modular.customer.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.annotion.Permission;
import com.stylefeng.guns.core.common.constant.Const;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.common.exception.BizExceptionEnum;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.support.BeanKit;
import com.stylefeng.guns.modular.customer.dao.CustomerCompanyManageDao;
import com.stylefeng.guns.modular.customer.service.ICustomerCompanyManageService;
import com.stylefeng.guns.modular.customer.util.CustomerUtil;
import com.stylefeng.guns.modular.customer.warpper.CustomerCompanyManageWrapper;
import com.stylefeng.guns.modular.system.model.CustomerCompanyManage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.naming.NoPermissionException;
import java.util.List;
import java.util.Map;

/**
 * 业务管理控制器
 *
 * @author fengshuonan
 * @Date 2018-04-02 11:42:34
 */
@Controller
@RequestMapping("/customerCompanyManage")
public class CustomerCompanyManageController extends BaseController {

    private String PREFIX = "/customer/customerCompanyManage/";

    @Autowired
    private ICustomerCompanyManageService customerCompanyManageService;
    @Autowired
    private CustomerCompanyManageDao customerCompanyManageDao;

    /**
     * 跳转到业务管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "customerCompanyManage.html";
    }

    /**
     * 跳转到添加业务管理
     */
    @RequestMapping("/customerCompanyManage_add")
    public String customerCompanyManageAdd() {
        return PREFIX + "customerCompanyManage_add.html";
    }

    /**
     * 跳转到修改业务管理
     */
    @RequestMapping("/customerCompanyManage_update/{customerCompanyManageId}")
    public String customerCompanyManageUpdate(@PathVariable Integer customerCompanyManageId, Model model) {
        CustomerCompanyManage customerCompanyManage = customerCompanyManageService.selectById(customerCompanyManageId);
        model.addAttribute("item", customerCompanyManage);
        LogObjectHolder.me().set(customerCompanyManage);
        return PREFIX + "customerCompanyManage_edit.html";
    }

    /**
     * 获取业务管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String customerTypeCode,
                       @RequestParam(required = false) String customerCompanyName) {

        Page<CustomerCompanyManage> page = new PageFactory<CustomerCompanyManage>().defaultPage();
        List<Map<String, Object>> list = customerCompanyManageDao.list(null, page, customerTypeCode, customerCompanyName);
        page.setRecords((List<CustomerCompanyManage>) new CustomerCompanyManageWrapper(list).warp());
        return super.packForBT(page);
    }

    /**
     * 新增业务管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission
    public Object add(@Validated CustomerCompanyManage customerCompanyManage) {
        customerCompanyManageService.insert(customerCompanyManage);
        return SUCCESS_TIP;
    }

    /**
     * 删除业务管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission(Const.ADMIN_NAME)
    public Object delete(@RequestParam Integer customerCompanyId) {
        customerCompanyManageService.deleteById(customerCompanyId);
        return SUCCESS_TIP;
    }

    /**
     * 修改业务管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission
    public Object update(@Validated CustomerCompanyManage customerCompanyManage) throws NoPermissionException {
        CustomerCompanyManage dbManage = this.customerCompanyManageService
                .selectById(customerCompanyManage.getCustomerCompanyId());
        if (dbManage == null) {
            return new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        if(!CustomerUtil.validEditPermission(dbManage.getCustomerCompanyId())){
            throw new NoPermissionException();
        };
        customerCompanyManageService.updateById(customerCompanyManage);
        return SUCCESS_TIP;
    }
    @RequestMapping(value = "/customerCompanyManage_detail/{customerCompanyId}")
    public Object toDetail(@PathVariable("customerCompanyId") Integer customerCompanyId, Model model) {
        List<Map<String, Object>> list =customerCompanyManageDao.selectByCompanyId(customerCompanyId);
        model.addAttribute("items", new CustomerCompanyManageWrapper(list).warp());
        return PREFIX + "customerCompanyManage_detail.html";
    }

    /**
     * 业务管理详情
     */
    @RequestMapping(value = "/detail/{customerCompanyManageId}")
    @ResponseBody
    public Object detail(@PathVariable("customerCompanyManageId") Integer customerCompanyManageId) {
        CustomerCompanyManage customerCompanyManage = customerCompanyManageService.selectById(customerCompanyManageId);
        if (customerCompanyManage == null) {
            throw new GunsException(BizExceptionEnum.DB_RESOURCE_NULL);
        }
        Map<String, Object> map = BeanKit.beanToMap(customerCompanyManage);

        if (CustomerUtil.validEditPermission(customerCompanyManage.getCustomerCompanyId())) {
            map.put(Const.AUTHORITY_FIELD_NAME, 1);
        } else {
            map.put(Const.AUTHORITY_FIELD_NAME, -1);
        }

        return new CustomerCompanyManageWrapper(map).warp();
    }
}
